package the.workshop.bu.remote;

import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.wisteria.framework.util.HttpUtils;
import org.wisteria.framework.util.MD5Utils;
import org.wisteria.framework.util.SerializeUtil;
import the.workshop.bu.dto.MobileEmptyDTO;
import the.workshop.bu.dto.MobileNameDTO;
import the.workshop.bu.dto.MobileStatusDTO;
import the.workshop.bu.dto.ServiceResult;

import java.util.HashMap;
import java.util.Map;

@Component
public class MobileCheckComponent {

    private static final String appId = "ygJYVO2SZASC2jKi";
    private static final String appSecurity = "ygJYVO2SZASC2jKiDNqYxvfSRDEVpJ2i";

    public ServiceResult<MobileEmptyDTO> checkEmpty(String mobile) {
        String timestamp = System.currentTimeMillis() + "";
        //参数
        //接口地址
        String url = "https://api.shumaidata.com/v4/mobile_empty/check";
        //请下载MD5Utils文件,地址为 https://file.tianyandata.cn/demo/utils/MD5Utils.java
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("mobile", mobile);

        //请下载 HttpUtils文件，地址为 https://file.tianyandata.cn/demo/utils/HttpUtils.java
        String text = HttpUtils.get(url,null,params);

        if (StringUtils.hasText(text)) {
            return SerializeUtil.deserialize(text, new TypeReference<ServiceResult<MobileEmptyDTO>>() {});
        }
        return null;
    }

    public ServiceResult<MobileStatusDTO> checkStatus(String mobile) {
        String timestamp = System.currentTimeMillis() + "";
        //接口地址
        String url = "https://api.shumaidata.com/v1/mobile_status/check";
        //请下载MD5Utils文件,地址为 https://file.tianyandata.cn/demo/utils/MD5Utils.java
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("mobile", mobile);

        //请下载 HttpUtils文件，地址为 https://file.tianyandata.cn/demo/utils/HttpUtils.java
        String text = HttpUtils.get(url,null,params);
        if (StringUtils.hasText(text)) {
            return SerializeUtil.deserialize(text, new TypeReference<ServiceResult<MobileStatusDTO>>() {});
        }
        return null;
    }

    public ServiceResult<MobileNameDTO> checkMobileAndName(String mobile, String name) {
        String timestamp = System.currentTimeMillis() + "";
        //接口地址
        String url = "https://api.shumaidata.com/v2/mobile_two/check";
        //请下载MD5Utils文件,地址为 https://file.tianyandata.cn/demo/utils/MD5Utils.java
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("name", name);
        params.put("mobile", mobile);

        //请下载 HttpUtils文件，地址为 https://file.tianyandata.cn/demo/utils/HttpUtils.java
        String text = HttpUtils.get(url,null,params);
        if (StringUtils.hasText(text)) {
            return SerializeUtil.deserialize(text, new TypeReference<ServiceResult<MobileNameDTO>>() {});
        }
        return null;
    }

    public static void main(String[] args) {
        String text = "{\"success\":true,\"code\":200,\"msg\":\"成功\",\"data\":{\"order_no\":\"679596455637121664\",\"area\":\"广东-深圳\",\"channel\":\"中国移动\",\"status\":1}}";
        //ServiceResult<MobileEmptyDTO> result = SerializeUtil.deserialize(text, new TypeReference<ServiceResult<MobileEmptyDTO>>() {});
        ServiceResult<MobileEmptyDTO> result = SerializeUtil.deserialize(text, ServiceResult.class);
        System.out.println(result.toString());

        String text3 = "{\"msg\":\"成功\",\"success\":true,\"code\":200,\"data\":{\"order_no\":\"389076819853615081\",\"fee\":1,\"result\":1}}";
        ServiceResult<MobileNameDTO> result3 = SerializeUtil.deserialize(text3, new TypeReference<ServiceResult<MobileNameDTO>>() {});
        System.out.println(result3.toString());
    }
}
